<?php

namespace backend\models\download;

use Yii;
use yii\data\Pagination;
use common\components\FileToken;

/**
 * This is the model class for table "edu_soft".
 *
 * @property integer $soft_id
 * @property string $soft_name
 * @property string $soft_desc
 * @property integer $softcate_id
 * @property string $soft_down_url
 * @property integer $soft_down_count
 * @property integer $soft_status
 * @property integer $soft_sort
 * @property integer $soft_addtime
 * @property integer $soft_updtime
 * @property integer $soft_size
 * @property integer $soft_points
 */
class Soft extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'edu_soft';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            //[['soft_name', 'soft_desc', 'softcate_id', 'soft_down_url', 'soft_status', 'soft_addtime', 'soft_updtime', 'soft_size', 'soft_points'], 'required'],
			[['soft_name', 'soft_desc', 'softcate_id', 'soft_status', 'soft_points'], 'required'],
            [['softcate_id', 'soft_down_count', 'soft_status', 'soft_sort', 'soft_addtime', 'soft_updtime', 'soft_points'], 'integer'],
            [['soft_name'], 'string', 'max' => 50],
            [['soft_desc', 'soft_down_url'], 'string', 'max' => 100]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'soft_id' => 'Soft ID',
            'soft_name' => '软件名称',
            'soft_desc' => '软件描述',
            'softcate_id' => '软件分类',
            'soft_down_url' => '下载地址',
            'soft_down_count' => 'Soft Down Count',
            'soft_status' => '软件状态',
            'soft_sort' => '软件排序',
            'soft_addtime' => 'Soft Addtime',
            'soft_updtime' => 'Soft Updtime',
            'soft_size' => '软件大小',
            'soft_points' => '所需积分',
        ];
    }
    
    /**
     * 软件列表 查询数据库生成列表分页
     */
    public function PagesTo() {
        $query = $this->find();
        $countQuery = clone $query;
        $pages = new Pagination(['totalCount' => $countQuery->count(),'pageSize' => '8']);
        $models = $query->offset($pages->offset)
        ->orderby('soft_updtime desc')
        ->limit($pages->limit)
        ->asArray()
        ->all();
        return ['models' => $models, 'pages' => $pages];
    }
    
    /**
     * 软件上传  软件信息入库
     */
    public function Softadd() {
        $request = Yii::$app->request;
        $this->load(Yii::$app->request->post());
        $this->soft_addtime = time();
        $this->soft_updtime = time();
        $this->soft_size = ($request->post('file_size')/1024);
        //$url = "http://files.bwphp.com/";
        //$this->soft_down_url = $url.$request->post('file_name');
        //$this->soft_thumb = $url.$request->post('image_name');
        $this->soft_down_url = $request->post('file_name');
        $this->soft_thumb = $request->post('image_name');
        $this->save();
        $softid = $this->attributes['soft_id'];
        foreach ($request->post('course_id') as $key => $value) {
            $newdata[] = array('soft_id'=>$softid,'course_id'=>$value);
        }
        return $newdata;
    }

	/**
	 * 软件上传 查询软件分类、相关课程
	 */
    public function UploadTo(){
        $softcat = (new \yii\db\Query())->from('edu_softcate')->all();
        foreach ($softcat as $key => $value) {
            $newdata[$value['softcate_id']] = $value['softcate_name'];
        }
        
        $course = (new \yii\db\Query())->select(['course_id','course_name'])->from('edu_course')->where(['status'=>1])->orderBy('upd_time DESC')->all();
        foreach ($course as $key => $value) {
            $newcourse[$value['course_id']] = $value['course_name'];
        }
        
        $view['softcat'] = $newdata;
        $view['course'] = $newcourse;
        $view['model'] = new Soft;
        $view['upmodel'] = new UploadForm;
        return $view;
    }
    
	/**
	 *软件编辑 软件信息
	 */
	 public function Setsoft() {				
		return (new \yii\db\Query())
			->where(['soft_id' =>Yii::$app -> request -> get('id')])
			->from('edu_soft')->one();		
	 }
	 
	 /**
	  * 软件编辑 信息入库
	  */
	 public function Updsoftto () {
	      $id = Yii::$app -> request->get('id');
	      $soft = $this->find()->where(['soft_id'=>$id])->one();
	      $soft->load(Yii::$app->request->post());
	      $soft->soft_updtime = time();
	      return $soft->save();	
	      
	     /* $cate = $this -> findOne(Yii::$app->request->get('id'));
	      $cate->load(Yii::$app->request->post());
	      $cate->softcate_updtime = time();
	      return $cate->save();*/
	 }
    
	/**
	 * 软件编辑 查询相关课程、分类
	 */
	 public function SetsoftData() {
	     $softcat = (new \yii\db\Query())->from('edu_softcate')->all();
	     foreach ($softcat as $key => $value) {
	         $newdata[$value['softcate_id']] = $value['softcate_name'];
	     }
	     
	     $course = (new \yii\db\Query())->select(['course_id','course_name'])->from('edu_course')->all();
	     foreach ($course as $key => $value) {
	         $newcourse[$value['course_id']] = $value['course_name'];
	     }
	     
	     $view['softcat'] = $newdata;
	     $view['course'] = $newcourse;
	     $view['model'] = new Soft;
	     $view['upmodel'] = new UploadForm;
	     $view['info'] = $view['model'] -> Setsoft();
	     $view['softcourse'] = $view['model'] -> Setsoftcourse();
	     return $view;
	 }
	 
	/**
	 *软件编辑 软件相关课程
	 */
	 public function Setsoftcourse() {				
		$arr = (new \yii\db\Query())
			->select('course_id')
			->where(['soft_id' =>Yii::$app -> request -> get('id')])
			->from('edu_soft_course')->all();
		foreach($arr as $val){
			$newarr[] = $val['course_id'];
		}
		return $newarr;
	 }
	 
	

	/**
	 *软件编辑 软件删除数据库数据
	 */
	 public function SoftdelData() {	
	    $id = Yii::$app->request->post();
	    $arr = $this->find()
	                ->select(['soft_down_url','soft_thumb'])
	                ->where(['in','soft_id',$id['delid']])
	                //->createCommand()->getRawSql();
	                ->asArray()    
	                ->all();
	    foreach ($arr as $key=>$val){
            $soft_token = FileToken::getToken($val['soft_down_url']);
            $thumb_token = FileToken::getToken($val['soft_thumb']);

            //请求删除接口
	        file_get_contents('http://files.bwphp.com/file/soft/unlink/'.$soft_token.'?path='.$val['soft_down_url']);
	        file_get_contents('http://files.bwphp.com/file/soft/unlink/'.$thumb_token.'?path='.$val['soft_thumb']);
	    }
		return $this->deleteAll(['soft_id' => $id['delid']]);
	 }

	
}
